<?php
class CoachModel extends Model {

	protected $tableName = 'coach';
	protected $fields = array(
			0=>'id',
			1=>'uid',
			2=>'level',
			3=>'sex',
			4=>'birth',
			5=>'sport',
			6=>"photo",
			7=>"diploma_photo",
			8=>"apply_status",
			9=>"apply_time",
			10=>"realname",
	);

	public function getCoathList($limit = 20, $map = array(),$order = "id DESC"){
		// 添加用户表的查询，用于关联查询
		
		if (isset ( $_POST )) {
			$_POST ['id'] && $map ['id'] = intval ( $_POST ['id'] );
			$_POST ['uid'] && $map ['uid'] = t ( $_POST ['uid'] );
			isset($_POST ['sex']) && $map ['sex'] = t ( $_POST ['sex'] );
			isset($_POST ['level']) && $map ['level'] = t ( $_POST ['level'] );
			$_POST ['sport'] && $map ['sport'] = t ( $_POST ['sport'] );
			
			isset($_POST ['apply_status']) && $map ['apply_status'] = t ( $_POST ['apply_status'] );
			$_POST ['realname'] && $map ['realname'] = t ( $_POST ['realname'] );
			// 注册时间判断，apply_time为数组格式
			if (! empty ( $_POST ['apply_time'] )) {
				if (! empty ( $_POST ['apply_time'] [0] ) && ! empty ( $_POST ['apply_time'] [1] )) {
					// 时间区间条件
					$map ['apply_time'] = array (
							'BETWEEN',
							array (
									strtotime ( $_POST ['apply_time'] [0] ),
									strtotime ( $_POST ['apply_time'] [1] )
							)
					);
				} else if (! empty ( $_POST ['apply_time'] [0] )) {
					// 时间大于条件
					$map ['apply_time'] = array (
							'GT',
							strtotime ( $_POST ['apply_time'] [0] )
					);
				} elseif (! empty ( $_POST ['apply_time'] [1] )) {
					// 时间小于条件
					$map ['apply_time'] = array (
							'LT',
							strtotime ( $_POST ['apply_time'] [1] )
					);
				}
			}

			// 生日时间，bitrh为数组格式
			if (! empty ( $_POST ['bitrh'] )) {
				if (! empty ( $_POST ['bitrh'] [0] ) && ! empty ( $_POST ['bitrh'] [1] )) {
					// 时间区间条件
					$map ['bitrh'] = array (
							'BETWEEN',
							array (
									strtotime ( $_POST ['bitrh'] [0] ),
									strtotime ( $_POST ['bitrh'] [1] )
							)
					);
				} else if (! empty ( $_POST ['bitrh'] [0] )) {
					// 时间大于条件
					$map ['bitrh'] = array (
							'GT',
							strtotime ( $_POST ['bitrh'] [0] )
					);
				} elseif (! empty ( $_POST ['bitrh'] [1] )) {
					// 时间小于条件
					$map ['bitrh'] = array (
							'LT',
							strtotime ( $_POST ['bitrh'] [1] )
					);
				}
			}
		}
		// 查询数据
		$list = $this->where ( $map )->order ( $order )->findPage ( $limit );
// 		dump($list);
// 		dump($this->getDbError());
		
		return $list;
	
	}
}
